package com.superatom.designpattern.proxy;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class GClibProxyStar implements MethodInterceptor {

    public Object getProxy(Class clazz){
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(clazz);
        enhancer.setCallback(this);
        return enhancer.create();
    }

    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("订票");
        if(method.getName().equals("sing")){
            // 调用返回值
            System.out.println("我在唱歌");
        }
        System.out.println("收钱");
        return o;
    }

    public static void main(String[] args) {
        GClibProxyStar gClibProxyStar = new GClibProxyStar();
        RealStar realStar = (RealStar) gClibProxyStar.getProxy(RealStar.class);
        realStar.sing();
    }
}
